package com.basic.library.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.recyclerview.widget.RecyclerView;

import com.basic.library.Interface.MCallback;
import com.basic.library.binding.viewadapter.recyclerview.MLayoutManagers;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE;

/**
 * @author yj
 * @description
 */
public class AutoScrollRecyclerView extends FrameLayout implements LifecycleObserver {
    private Disposable mAutoTask;
    private int current;
    RecyclerView recyclerView;
    View viewCover;
    ComponentActivity activity;

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public int getCurrent() {
        return current;
    }

    public AutoScrollRecyclerView(Context context) {
        this(context, null);
    }

    public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        activity = (ComponentActivity) context;
        recyclerView = new RecyclerView(context);
        recyclerView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(recyclerView);
        viewCover = new View(context);
        viewCover.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(viewCover);
        viewCover.setOnClickListener(v -> {
            if (recyclerView != null && recyclerView.getAdapter() != null) {
                total = recyclerView.getAdapter().getItemCount();
            }
            if (mCallback != null && total > 0)
                mCallback.onResponse(v.getId());
            else if (mCallbackEmpty != null && total == 0)
                mCallbackEmpty.onResponse(v.getId());
        });
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == SCROLL_STATE_IDLE && total > 1 && current == total - 1) {
                    current = 0;
                    recyclerView.scrollToPosition(current);
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        activity.getLifecycle().addObserver(this);
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        recyclerView.setLayoutManager(MLayoutManagers.mLinear1().create(recyclerView));
        recyclerView.setAdapter(adapter);
    }

    int total;
    boolean isScroll = true;

    public void start() {
        stop();
        mAutoTask = Observable.interval(4000, 3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (recyclerView != null && recyclerView.getAdapter() != null) {
                            total = recyclerView.getAdapter().getItemCount();
                        }
                        if (isScroll && total > 1) {
                            current++;
                            if (current >= total)
                                current = 0;
                            if (current < total) {
                                if (recyclerView != null)
                                    recyclerView.smoothScrollToPosition(current);
                            }
                        }
                    }
                });
    }

    public void stop() {
        if (mAutoTask != null && !mAutoTask.isDisposed()) {
            mAutoTask.dispose();
            mAutoTask = null;
        }
    }

    MCallback<Integer> mCallback;
    MCallback<Integer> mCallbackEmpty;

    public void addOnClickBack(MCallback<Integer> mCallback) {
        this.mCallback = mCallback;
    }

    public void addOnEmptyClickBack(MCallback<Integer> mCallback) {
        this.mCallbackEmpty = mCallback;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    void onResume() {
        start();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onPause() {
        stop();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    void onDestroy() {
        activity.getLifecycle().removeObserver(this);
    }

}
